package com.asiainfo.hybridapp.test;

import android.support.annotation.NonNull;

import com.framework.utils.ThreadPoolUtil;
import com.framework.utils.ToastUtil;
import com.framework.utils.multyprocessprovider.provider.utils.LogUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WifiScanUtil {

    private static volatile WifiScanUtil singleton;

    private WifiScanUtil() {
    }

    public static WifiScanUtil getInstance() {
        if (singleton == null) {
            synchronized (WifiScanUtil.class) {
                if (singleton == null) {
                    singleton = new WifiScanUtil();
                }
            }
        }
        return singleton;
    }

    public interface IScanResult {
        void onScanSuccess();

        void onScanError(Exception e);
    }

    public void scan(@NonNull final String deviceIp, final IScanResult iScanResult) {
        ThreadPoolUtil.getInstanceScheduledTaskExecutor().submit(new Runnable() {
            @Override
            public void run() {
                try {
                    Document doc = Jsoup.connect("http://" + deviceIp).get();
                    String text = doc.body().text();
                    LogUtils.e("TF卡内容==text==" + text);
                    Elements links = doc.getElementsByTag("a");
                    ArrayList<TfInfo> tfInfos = new ArrayList<>();
                    for (Element link : links) {
                        String linkHref = link.attr("href");
                        String linkText = link.text();
                        LogUtils.e("TF卡内容==linkHref==" + linkHref);
                        LogUtils.e("TF卡内容==linkText==" + linkText);
                        TfInfo tfInfo = new TfInfo();
                        tfInfo.setId(linkText);
                        tfInfo.setValue(linkHref);
                        tfInfos.add(tfInfo);
                        LogUtils.e("TF卡内容==links_size==" + links.size());
                        LogUtils.e("TF卡内容==tfInfos_size==" + tfInfos.size());
                        if (links.size() == tfInfos.size()) {
                            if (iScanResult != null) {
                                iScanResult.onScanSuccess();
                            }
                            ToastUtil.getInstance().showToast("跳转到TF卡页面");
                            //跳转到TF卡页面
//                            if (loadingDialog != null) {
//                                loadingDialog.dismiss();
//                            }
//                            TfActivity.startTfActivity(RecorderShowActivity.this, tfInfos);
                        }
                    }
                } catch (IOException e) {
                    if (iScanResult != null) {
                        iScanResult.onScanError(e);
                    }
                    e.printStackTrace();
                    LogUtils.e("==TF卡==" + e.getMessage());
                    e.printStackTrace();
                    ToastUtil.getInstance().showToast("请检查TF卡是否已插入");
//                    if (loadingDialog != null) {
//                        loadingDialog.dismiss();
//                    }
//                    ToastUtil.show(RecorderShowActivity.this, "请检查TF卡是否已插入");
//                    tfInfos = new ArrayList<>();
//                    TfActivity.startTfActivity(RecorderShowActivity.this, tfInfos);
                }
            }
        });
    }

    /**
     * 说明：path:  循环视频： path = "/video/";
     * 紧急视频： path = "/sos/";
     * 停车视频：path = "/wonderful/";
     * 图片： path = "/picture/";
     */
    public void getDocInfo(@NonNull final String deviceIp, @NonNull final String path, final String content,
                           final IScanResult iScanResult) {
        ThreadPoolUtil.getInstanceScheduledTaskExecutor().submit(new Runnable() {
            @Override
            public void run() {
                Document doc = null;
                try {
                    doc = Jsoup.connect("http://" + deviceIp + path).get();
                    String text = doc.body().text();
                    LogUtils.e("TF卡内容==text==" + text);
                    //Elements links = doc.getElementsByTag("a");
                    String[] links = text.split("-rw-");
                    ArrayList tfVideos = new ArrayList<>();
                    for (int i = 0; i < links.length; i++) {
                        String linkTexts = links[i];
                        if (linkTexts.contains(content)) {
                            Pattern p = Pattern.compile("\\s+");
                            Matcher m = p.matcher(linkTexts);
                            String linkText = m.replaceAll(" ");
                            String[] tfs = linkText.split(" ");
                            LogUtils.e("==tfs==0=" + tfs[0]);//
                            LogUtils.e("==tfs==1=" + tfs[1]);//
                            LogUtils.e("==tfs==2=" + tfs[2]);//视频大小
                            LogUtils.e("==tfs==3=" + tfs[3]);//
                            LogUtils.e("==tfs==4=" + tfs[4]);//
                            LogUtils.e("==tfs==5=" + tfs[5]);//
                            LogUtils.e("==tfs==6=" + tfs[6]);//文件名称包含时间

                            TfInfo tfInfo = new TfInfo();
                            tfInfo.setSize(tfs[2]);
                            int type = 0;
                            if (type == 3) {
                                tfInfo.setType(TfInfo.PICTURE);
                            } else {
                                tfInfo.setType(TfInfo.VIDEO);
                            }
                            tfInfo.setUrl("http://" + deviceIp + path + tfs[6]);
                            String ymd = tfs[6].split("_")[0];
                            String[] days = tfs[6].split("_")[1].split("-");
                            String day = days[0] + ":" + days[1] + ":" + days[2];
                            tfInfo.setTime(ymd + " " + day);
                            tfInfo.setDayTime(day);
                            tfInfo.setValue(tfs[6]);
                            tfInfo.setPath(path);
                            tfVideos.add(tfInfo);
                        }
                        if (i == links.length - 1) {
                            if (iScanResult != null) {
                                iScanResult.onScanSuccess();
                            }
//                            if (tfVideos.size() == 0) {
//                                if (loadingDialog != null) {
//                                    loadingDialog.dismiss();
//                                }
//                                ll_parent.onRefreshComplete();
//                            }
//                            handler.sendEmptyMessage(HANDVIDEO);

                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    if (iScanResult != null) {
                        iScanResult.onScanError(e);
                    }
                    LogUtils.e("TF报错==video===" + e.getMessage());
                }
            }
        });
    }

    //http://" + BaseApplication.ip + path+文件名称。
    public void openVideo() {

    }
}
